import Vuex from 'vuex'
import defulatState from './state/state.js'
import mutations from './mutations/mutations.js'
import getters from './getters/getters.js'
import actions from './actions/actions.js'
export default () => {
  const store = new Vuex.Store({
    strict: process.env.NODE_ENV === 'development',
    state: defulatState,
    mutations,
    getters,
    actions,
    plugins: [
      (store) => {
        console.log('my plguin invoked')
      }
    ],
    modules: {
      a: {
        namespaced: true,
        state: {
          text: 1
        },
        mutations: {
          // 此处的state为a模块的state
          updateText (state, text) {
            console.log('text:' + text)
            state.text = text
          }
        },
        getters: {
          textPlus (state, getters, rootState) {
            return state.text + rootState.b.text
          }
        },
        actions: {
          add ({ state, commit, rootState }) {
            console.log(rootState)
            commit('updateText', rootState.count)
            commit('updateCount', { num1: 5678910 }, { root: true })
          }
        }
      },
      b: {
        namespaced: true,
        state: {
          text: 2
        },
        actions: {
          testMutationA ({ commit }) {
            commit('a/updateText', 'textatexta', { root: true })
          }
        }
      }
    }
  })
  if (module.hot) {
    module.hot.accept(['./state/state', './mutations/mutations', './getters/getters',
      './actions/actions'], () => {
    // 获取更新后的模块
    // 因为 babel 6 的模块编译格式问题，这里需要加上 `.default`
      const newState = require('./state/state').default
      const newMutations = require('./mutations/mutations').default
      const newGetters = require('./getters/getters').default
      const newActions = require('./actions/actions').default
      store.hotUpdate({
        state: newState,
        mutations: newMutations,
        actions: newActions,
        getters: newGetters
      })
    })
  }
  return store
}
